import tkinter as tk
from tkinter import messagebox
from page.Config import Config

class LoginWindow:
    def __init__(self, master):
        self.master = master
        master.title("登录验证")

        # 账号标签和输入框
        self.label_username = tk.Label(master, text="账号:")
        self.label_username.grid(row=1,column=1)
        self.entry_username = tk.Entry(master)
        self.entry_username.grid(row=1,column=2)
        
        # 密码标签和输入框
        self.label_password = tk.Label(master, text="密码:")
        self.label_password.grid(row=2,column=1)
        self.entry_password = tk.Entry(master, show="*")
        self.entry_password.grid(row=2,column=2)
        
        # 登录按钮
        self.login_button = tk.Button(master, text="登录", command=self.check_login)
        self.login_button.grid(row=3,column=2)
    
    def check_login(self):
        username = self.entry_username.get()
        password = self.entry_password.get()
        # 实例化
        config = Config()
        # 简单验证逻辑
        if username == config.data['username'] and password == config.data['password']:
            messagebox.showinfo("登录成功", "欢迎回来，管理员！")
            config.data['loginStatus'] = True
            config.dump()
            self.master.destroy()
        else:
            messagebox.showerror("登录失败", "账号或密码错误！")

    def open_login_window():
        login_root = tk.Toplevel()
        # 设置窗口大小
        login_root.geometry("400x300")  
        login_root.title('登录')
        # 设置grid布局 4x4
        for row in range(4):
            login_root.rowconfigure(row, weight=1)
        for col in range(4):        
            login_root.columnconfigure(col, weight=1)   
        login_app = LoginWindow(login_root)
     